﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.WorkFlow.Template
{
    /// <summary>
    /// 数据访问类WorkFlowClass。
    /// </summary>
    public class WorkFlowClass
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "WFClassId", "Caption", "FatherId", "Description", "ClassLevel", "MgrUrl" };
        private string tableName = "HF_WorkFlowClass";
        private string keyField = "WFClassId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(WorkFlowClassEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@WFClassId", entity.WFClassId, typeof(string));
            sqlDataItem.AppendParameter("@Caption", entity.Caption, typeof(string));
            sqlDataItem.AppendParameter("@FatherId", entity.FatherId, typeof(string));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
            sqlDataItem.AppendParameter("@ClassLevel", entity.ClassLevel, typeof(int));
            sqlDataItem.AppendParameter("@MgrUrl", entity.MgrUrl, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public WorkFlowClassEntity GetWorkFlowClassEntity(DataRow dr)
        {
            WorkFlowClassEntity entity = new WorkFlowClassEntity();
            string value = "";
            entity.WFClassId = dr["WFClassId"].ToString();
            entity.Caption = dr["Caption"].ToString();
            entity.FatherId = dr["FatherId"].ToString();
            entity.Description = dr["Description"].ToString();
            entity.MgrUrl = dr["MgrUrl"].ToString();
            value = dr["ClassLevel"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.ClassLevel = Convert.ToInt32(value);
            }
            return entity;
        }

        #endregion

        #region  构造函数
        public WorkFlowClass()
        { }
        public static WorkFlowClass GetInstance()
        {
            return new WorkFlowClass();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(WorkFlowClassEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(WorkFlowClassEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string wFClassId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, wFClassId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="wFClassId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string wFClassId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, wFClassId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="WFClassId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetWorkFlowClassTable(string wFClassId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, wFClassId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="wFClassId">主键Id</param>
        /// <returns>DataTable</returns>
        public WorkFlowClassEntity GetWorkFlowClassEntity(string wFClassId)
        {
            DataTable dt = GetWorkFlowClassTable(wFClassId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetWorkFlowClassEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法

        /// <summary>
        /// 获得流程分类的所有子分类列表
        /// </summary>
        /// <param name="classId">分类Id</param>
        /// <returns>子分类table</returns>
        public  DataTable GetChildClass(string classId)
        {
            try
            {
                string tmpStr = "select * from HF_WorkFlowClass where  FatherId=@FatherId";//有效的树信息
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@FatherId", classId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
  
        /// <summary>
        /// 获得流程分类的所有子分类列表
        /// </summary>
        /// <param name="classId">分类Id</param>
        /// <returns>子分类Entitys</returns>
        public  List<WorkFlowClassEntity>  GetChildClassEntitys(string classId)
        {
            try
            {
                List<WorkFlowClassEntity> result = new List<WorkFlowClassEntity>();
                DataTable dt = GetChildClass(classId);
                foreach (DataRow dr in dt.Rows)
                {
                    WorkFlowClassEntity entity = GetWorkFlowClassEntity(dr);
                    result.Add(entity);
                }
               return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
        #endregion
    }
}

